package com.ceshiren.mini.controller;

import com.ceshiren.mini.dto.PlanDTO;
import com.ceshiren.mini.dto.PlanTestCasesDTO;
import com.ceshiren.mini.dto.TestCaseDTO;
import com.ceshiren.mini.service.PlanService;
import com.ceshiren.mini.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

import static com.ceshiren.mini.util.ResultCode.*;

@RestController
@RequestMapping("plan")
@Api(tags = "测试计划管理")
public class PlanController {


    private PlanService planService;

    @Autowired
    public void setPlanService(PlanService planService) {
        this.planService = planService;
    }


    //测试计划的创建
    @ApiOperation(value = "新增测试计划")
    @PostMapping
    public R createTask(@RequestBody PlanDTO planDTO){
        //testTaskAddTDO 不能为空
        if(Objects.isNull(planDTO)){
            return R.error(PARAMETER_ERROR).message("测试计划为空");
        }
        //name 测试计划名称 不能为空
        if(!StringUtils.hasText(planDTO.getName())){
            return R.error(PARAMETER_ERROR).message("测试计划名称不能为空");
        }
        //[1,4,5]  [1,4] []
        List<Integer> testCaseListId = planDTO.getTestcase_ids();
        int size = testCaseListId.size();//测试用例的个数
        if(0 == size || Objects.isNull(testCaseListId)){
            return R.error(PARAMETER_ERROR).message("用例列表不能为空");
        }
        //一个计划关联了几条测试用例的个数
        int insertTaskNum = planService.createPlan(planDTO);
        if(insertTaskNum > 0){
            if(insertTaskNum == size)
                return R.ok().message("当前测试计划的所有测试用例关联成功");
            else
                return R.ok()
                        .message("当前测试计划需要关联" + size +"个测试用例,成功关联了" + insertTaskNum + "个测试用例");

        }
        return R.error(PARAMETER_ERROR).data(planDTO);
    }

    @ApiOperation(value = "查询测试计划")
    @GetMapping
    public R getPlan(@RequestParam(name = "id", required = false) Integer planId){
        if(null == planId){
            //获取所有的测试计划
            List<PlanTestCasesDTO> planDTOList = planService.getPlanList();
            //返回的实体类如果是空，null  error
            return (planDTOList == null || planDTOList.isEmpty())
                    ? R.error(PARAMETER_NOT_EXIST).message("没有测试用例")
                    : R.ok().data(planDTOList);
        }else {
            //根据ID获取测试计划
            PlanTestCasesDTO planTestCasesDTO = new PlanTestCasesDTO();
            planTestCasesDTO.setId(planId);
            System.out.println("planTestCasesDTO:" + planTestCasesDTO);
            PlanTestCasesDTO planTestCasesDTO1 = planService.getPlanTestCasesDTO(planTestCasesDTO);

            //返回的实体类如果是空，null  error
            if(Objects.isNull(planTestCasesDTO1))
                return R.error(PARAMETER_NOT_EXIST).message("计划不存在");
            return R.ok().data(planTestCasesDTO1);
        }
    }

    //删除测试用例
    @ApiOperation(value = "测试计划的删除")
    @DeleteMapping
    public R deletePlan(@RequestBody PlanDTO planDTO){

        int deleted = planService.deletePlan(planDTO);
        return deleted < 0
                ? R.error(DELETEERROR)
                : R.ok().data(planDTO.getId());
    }
}
